package com.frontop.system.modules.app.service.impl;

import cn.hutool.core.io.IoUtil;
import com.frontop.system.modules.app.service.MongoFileService;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Query;
import com.frontop.system.modules.app.model.FileDocument;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author YangBoss
 * @title: FileServiceImpl
 * @projectName frontop-ao
 * @description: TODO
 * @date 2022/6/22 15:05
 */
@Service
public class FileServiceImpl implements MongoFileService {
    @Autowired
    private MongoTemplate mongoTemplate;
//    @Autowired
//    private GridFsTemplate gridFsTemplate;
    @Resource(name = "gridFsMp4Template")
    private GridFsTemplate gridFsTestTemplate;
    @Resource(name = "gridMp4BucketTemplate")
    private GridFSBucket gridFSBucket;

//    @Autowired
//    private GridFSBucket gridFSBucket;
    @Override
    public Optional<FileDocument> getFileById(String id) {
        FileDocument fileDocument = mongoTemplate.findById(id , FileDocument.class );
        if(fileDocument != null){
            Query gridQuery = new Query().addCriteria(Criteria.where("_id").is(new ObjectId(fileDocument.getGridfsId())));
            try {
                //根据id查询文件
                GridFSFile fsFile = gridFsTestTemplate.findOne(gridQuery);

                //打开流下载对象
                GridFSDownloadStream in = gridFSBucket.openDownloadStream(fsFile.getObjectId());
                if(in.getGridFSFile().getLength() > 0){
                    //获取流对象
                    GridFsResource resource = new GridFsResource(fsFile, in);
                    //获取数据
                    fileDocument.setContent(IoUtil.readBytes(resource.getInputStream()));
                    return Optional.of(fileDocument);
                }else {
                    fileDocument = null;
                    return Optional.empty();
                }
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }
        return Optional.empty();
    }


    @Override
    public byte[] downloadFile(String objectId, OutputStream out) throws Exception {

        return null;
    }



    private static Pattern NUMBER_PATTERN = Pattern.compile("(?<==).*(?=})");
    private String getFileId(String bsonObjectId) {
        Matcher m = NUMBER_PATTERN.matcher(bsonObjectId);
        if(!m.find()){
            return bsonObjectId;
        }
        return m.group();
    }
}
